package cn.edu.lcvc.ebuy_23rj2.web.controller.backstage.adminmanage;


import cn.edu.lcvc.ebuy_23rj2.model.Admin;
import cn.edu.lcvc.ebuy_23rj2.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class AdminManageController {

    @Autowired
    private AdminService adminService;

    @RequestMapping(method = RequestMethod.GET,value="/backstage/adminmanage/list")
    public String list(Admin admin, HttpServletRequest request,HttpSession session){
        //1.从数据库中找到admin表的匹配查询结果的所有记录
        List<Admin> admins = adminService.getAdmins(admin);
        //2.将值传递到页面中
        request.setAttribute("list",admins);
        //跳转到管理员列表页面。
        return "/jsp/backstage/adminmanage/adminmanage.jsp";
    }

    @RequestMapping(method = RequestMethod.GET,value="/backstage/adminmanage/deleteAdmin")
    public String delete(Integer id,HttpSession session,HttpServletRequest request){
        if(id!=null){
            //获取自己当前的登录账号信息
            Admin admin=(Admin)session.getAttribute("admin");
            if(admin!=null){//已经登录
                if(admin.getId()!=id){//如果删除的账号不是当前登录的账号
                    adminService.deleteAdmin(id);
                }else{//如果删除的是自己
                    request.setAttribute("myMessage","删除失败：无法删除自己的账户！");
                }
            }
        }
        //执行删除操作后：需要重新读取数据库中现有的admin表中的所有记录
        //技巧：此处跳转到另一个控制器的地址
        return "/backstage/adminmanage/list";
    }

    @RequestMapping(method = RequestMethod.GET,value="/backstage/adminmanage/toAddAdmin")
    public String toAdd(){
        return "/jsp/backstage/adminmanage/adminadd.jsp";
    }

    @RequestMapping(method = RequestMethod.POST,value="/backstage/adminmanage/doAddAdmin")
    public String doAdd(Admin admin,HttpServletRequest request){
        //1.接受表单传递过来的值
        //2.保存进数据库
        //（1）先判断账户名是否重名
        if(admin.getUsername()==null||admin.getUsername().length()==0){//账户名不能为空
            request.setAttribute("myMessage","添加失败：账户名不能为空");
        }else if(admin.getName()==null||admin.getName().length()==0){//性能不能为空
            request.setAttribute("myMessage","添加失败：姓名不能为空");
        }else if(adminService.countUsername(admin.getUsername())){//如果重名
            request.setAttribute("myMessage","添加失败：账户名重名，请选择其他的账户名！");
        }else{//如果不重名
            adminService.saveAdmin(admin);
        }
        //3.跳转会添加页面
        return "/jsp/backstage/adminmanage/adminadd.jsp";
    }

    @RequestMapping(method = RequestMethod.GET,value="/backstage/adminmanage/toUpdateAdmin")
    public String toUpdate(Integer id,HttpServletRequest request){
        //根据id到数据库中查找对应的账户信息
        Admin admin=adminService.getAdminById(id);
        //将值传递到编辑页面中
        request.setAttribute("admin",admin);
        return "/jsp/backstage/adminmanage/adminupdate.jsp";
    }

    @RequestMapping(method = RequestMethod.POST,value="/backstage/adminmanage/doUpdateAdmin")
    public String doUpdate(Admin admin,HttpServletRequest request){
        if(admin.getUsername()==null||admin.getUsername().length()==0){//账户名不能为空
            request.setAttribute("myMessage","编辑失败：账户名不能为空");
        }else if(admin.getName()==null||admin.getName().length()==0){//性能不能为空
            request.setAttribute("myMessage","编辑失败：姓名不能为空");
        }else if(adminService.countByUsernameAndNotId(admin.getUsername(),admin.getId())){//如果重名
            request.setAttribute("myMessage","编辑失败：账户名重名，请选择其他的账户名！");
        }else{//如果不重名
            adminService.updateAdmin(admin);
        }
        //3.跳转回编辑页面
        //将值传递到编辑页面中
        request.setAttribute("admin",admin);
        return "/jsp/backstage/adminmanage/adminupdate.jsp";
    }
}
